<html>
<head><meta charset="utf-8"><title>Data Races in Go/Swift? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Data.20Races.20in.20Go.2FSwift.3F.html">Data Races in Go/Swift?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="182113794"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Data%20Races%20in%20Go/Swift%3F/near/182113794" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Data.20Races.20in.20Go.2FSwift.3F.html#182113794">(Nov 28 2019 at 14:42)</a>:</h4>
<p>I recently heard someone say that Data Races in Go are Undefined Behavior. Is that true? I was unable to find a definite answer in the Go docs, but <a href="http://nil.csail.mit.edu/6.824/2016/notes/gomem.pdf" target="_blank" title="http://nil.csail.mit.edu/6.824/2016/notes/gomem.pdf">this talk</a> says</p>
<blockquote>
<p>Races on interface, string, slice, map entries can still do arbitrarily bad things</p>
</blockquote>
<p>That is really surprising because Go is also widely considered a "safe" language! Java at least gives defined semantics to its data races to earn the "safe" label.<br>
And then I checked Swift and the situation superficially seems similar? Does anyone know more?</p>



<a name="182114506"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Data%20Races%20in%20Go/Swift%3F/near/182114506" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Data.20Races.20in.20Go.2FSwift.3F.html#182114506">(Nov 28 2019 at 14:53)</a>:</h4>
<p>I find it rather amusing (or concerning) that the <a href="https://golang.org/ref/mem" target="_blank" title="https://golang.org/ref/mem">Go memory model</a> literally says nothing about data races...</p>



<a name="182890595"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Data%20Races%20in%20Go/Swift%3F/near/182890595" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Muhammad Mominul Huque <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Data.20Races.20in.20Go.2FSwift.3F.html#182890595">(Dec 08 2019 at 12:23)</a>:</h4>
<p>"The Swift language guarantees memory safety in single threaded environments. However, conflicting accesses in multithreaded code lead to data races. "<br>
<a href="https://swift.org/blog/tsan-support-on-linux/" target="_blank" title="https://swift.org/blog/tsan-support-on-linux/">https://swift.org/blog/tsan-support-on-linux/</a></p>



<a name="183167770"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Data%20Races%20in%20Go/Swift%3F/near/183167770" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Data.20Races.20in.20Go.2FSwift.3F.html#183167770">(Dec 11 2019 at 15:29)</a>:</h4>
<p>sure, but are data races <em>UB</em> or are they handled gracefully (Java does the latter, AFAIK the same goes for C#)?</p>



<a name="183879984"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Data%20Races%20in%20Go/Swift%3F/near/183879984" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Data.20Races.20in.20Go.2FSwift.3F.html#183879984">(Dec 19 2019 at 20:01)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> did you ever answer this question to your satisfaction?</p>



<a name="184054304"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Data%20Races%20in%20Go/Swift%3F/near/184054304" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Data.20Races.20in.20Go.2FSwift.3F.html#184054304">(Dec 22 2019 at 13:15)</a>:</h4>
<p>I was pointed at this part of the Go docs:</p>
<blockquote>
<p>When multiple goroutines access a shared variable v, they must use synchronization events to establish happens-before conditions that ensure reads observe the desired writes. </p>
</blockquote>
<p>My conclusion is that data races in Go <em>are</em> UB, and that "Go is a safe language" is imprecise at best. One could also say it's really good marketing...</p>



<a name="186526907"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Data%20Races%20in%20Go/Swift%3F/near/186526907" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Data.20Races.20in.20Go.2FSwift.3F.html#186526907">(Jan 24 2020 at 19:45)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> This <a href="https://www.reddit.com/r/golang/comments/erjsks/im_aware_of_race_condition_but_should_i_care/ff4971p/" target="_blank" title="https://www.reddit.com/r/golang/comments/erjsks/im_aware_of_race_condition_but_should_i_care/ff4971p/">Reddit comment</a> from a "Go compiler engineer" seems to agree with that assessment. There are other users in that thread stating the same thing as well.</p>



<a name="186629010"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Data%20Races%20in%20Go/Swift%3F/near/186629010" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Data.20Races.20in.20Go.2FSwift.3F.html#186629010">(Jan 26 2020 at 16:59)</a>:</h4>
<p>Thanks for the pointer!</p>



<a name="186629062"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Data%20Races%20in%20Go/Swift%3F/near/186629062" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Data.20Races.20in.20Go.2FSwift.3F.html#186629062">(Jan 26 2020 at 17:00)</a>:</h4>
<p>I also got confirmation from a former Uber engineer that Swift has exactly the same issue -- they are compiling code assuming no data races, and if there are data races, arbitrary things can go wrong</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>